/**
 * 
 */
package com.armail.server.rs;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.armail.server.dao.MailDAO;
import com.armail.server.dao.UserDAO;
import com.armail.server.dao.impl.MailDAOImpl;
import com.armail.server.dao.impl.UserDAOImpl;
import com.armail.server.domain.Mail;
import com.armail.server.domain.User;

/**
 * @author Arpit.Rathore
 *
 */
@Path("mails")
public class MailResource {
	
	private static MailDAO mailDAO = MailDAOImpl.getInstance();
	private static UserDAO userDAO = UserDAOImpl.getInstance();
	
	@Path("/getAllmails/{id}")
    @GET
    @Produces(MediaType.APPLICATION_XML)
	public Response getMails(@PathParam("id") Long id) {
		User user = userDAO.findById(id);
		final GenericEntity<List<Mail>> entity = new GenericEntity<List<Mail>>(mailDAO.getMailsByUser(user)) { };
		return Response.ok().entity(entity).build();
	}
	
	@Path("/getSentItems/{id}")
    @GET
    @Produces(MediaType.APPLICATION_XML)
	public Response getSentItems(@PathParam("id") Long id) {
		User user = userDAO.findById(id);
		final GenericEntity<List<Mail>> entity = new GenericEntity<List<Mail>>(mailDAO.getSentItemsByUser(user)) { };
		return Response.ok().entity(entity).build();
	}
	
	@Path("/getDraftmails/{id}")
    @GET
    @Produces(MediaType.APPLICATION_XML)
	public Response getDraftMails(@PathParam("id") Long id) {
		User user = userDAO.findById(id);
		final GenericEntity<List<Mail>> entity = new GenericEntity<List<Mail>>(mailDAO.getDraftMailsByUser(user)) { };
		return Response.ok().entity(entity).build();
	}
	
	@Path("/send")
	@POST
	public Mail sendMail(Mail mail) {
		User user = userDAO.findByUsername(mail.getToUser());
		mail.setOwner(user);
		return mailDAO.createMail(mail);
	}
}
